<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展 
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------

namespace app\admin\model\wddj;

use addons\wddj\library\IDCardEncryptor;
use app\admin\model\wddj\user\Wechat;
use think\Model;


class CertifiedDrivingAgent extends Model
{

    

    

    // 表名
    protected $name = 'wddj_certified_driving_agent';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'driving_experience_text',
        'driver_license_type_text',
        'state_text',
        'status'
    ];

    protected static function init()
    {
        self::beforeInsert(function ($row) {
            $row->mobile = (new Wechat())->where('id',$row['wechat_id'])->value('mobile');
        });
    }
    

    
    public function getDrivingExperienceList()
    {
        return ['1年' => __('Driving_experience 1年'), '2年' => __('Driving_experience 2年'), '3年' => __('Driving_experience 3年'), '4年' => __('Driving_experience 4年'), '5年' => __('Driving_experience 5年'), '5年以上' => __('Driving_experience 5年以上')];
    }

    public function getDriverLicenseTypeList()
    {
        return ['A1' => __('Driver_license_type A1'), 'A2' => __('Driver_license_type A2'), 'A3' => __('Driver_license_type A3'), 'B1' => __('Driver_license_type B1'), 'B2' => __('Driver_license_type B2'), 'C1' => __('Driver_license_type C1'), 'C2' => __('Driver_license_type C2')];
    }

    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }


    public function getDrivingExperienceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['driving_experience']) ? $data['driving_experience'] : '');
        $list = $this->getDrivingExperienceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDriverLicenseTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['driver_license_type']) ? $data['driver_license_type'] : '');
        $list = $this->getDriverLicenseTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusAttr($value, $data)
    {
        $driverObj = (new Driver())->where('wechat_id',$data['wechat_id'])->find();
        if (!empty($driverObj)) {
            $list = $this->getStatusList();
            $value = $driverObj['status'];
            return isset($list[$value]) ? $list[$value] : '';
        } else {
            return '';
        }

    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $driverObj = (new Driver())->where('wechat_id',$data['wechat_id'])->find();
        if (!empty($driverObj)) {
            $list = $this->getStatusList();
            $value = $driverObj['status'];
            return isset($list[$value]) ? $list[$value] : '';
        } else {
            return '';
        }

    }




    public function wechat()
    {
        return $this->belongsTo('app\admin\model\wddj\user\Wechat', 'wechat_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getIdCardAttr($value)
    {
        $configObj = (new \app\api\model\wddj\Config())->get(1);
        $encryptor = new IDCardEncryptor($configObj['encryptor_key'],$configObj['encryptor_iv']);
        $value = $encryptor->decrypt($value);
        return $value;
    }
}
